home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 April: Mac OS SDK / Dev.CD Apr 97 SDK1.toast / Development Kits (Disc 1) / QuickDraw 3D / Samples / SampleCode / Picking Mesh ShapeParts / Sources / PickMeshShapePartSupport.c < prev   
Encoding:
Text File  |  1996-05-21  |  7.8 KB  |  299 lines  |  [TEXT/CWIE]

  1. // PickMeshShapePartSupport.c - QuickDraw 3d routines
  2. //
  3. // written by Nick Thompson.
  4. // modifed by Robert Dierkes for an example of mesh shape part picking.
  5. // 
  6. // ©1994-95 Apple computer Inc., All Rights Reserved
  7. //
  8.  
  9. #include <QuickDraw.h>
  10.  
  11. #include "PickMeshShapePartSupport.h"
  12.  
  13.  
  14. #include "QD3DDrawContext.h"
  15. #include "QD3DRenderer.h"
  16. #include "QD3DShader.h"
  17. #include "QD3DCamera.h"
  18. #include "QD3DLight.h"
  19. #include "QD3DGeometry.h"
  20. #include "QD3DGroup.h"
  21. #include "QD3DSet.h"
  22. #include "QD3DView.h"
  23. #include "QD3DMath.h"
  24. #include "QD3DAcceleration.h"
  25.  
  26.  
  27. #define uMath_Sin_Deg(x)        ((float)sin((double)Q3Math_DegreesToRadians((x))))
  28. #define uMath_Cos_Deg(x)        ((float)cos((double)Q3Math_DegreesToRadians((x))))
  29.  
  30.  
  31.  
  32. TQ3ViewObject MyNewView(WindowPtr theWindow)
  33. {
  34.     TQ3Status                myStatus;
  35.     TQ3ViewObject            myView;
  36.     TQ3DrawContextObject    myDrawContext;
  37.     TQ3RendererObject        myRenderer;
  38.     TQ3CameraObject            myCamera;
  39.     TQ3GroupObject            myLights;
  40.     
  41.     myView = Q3View_New();
  42.     
  43.     //    Create and set draw context.
  44.     if ((myDrawContext = MyNewDrawContext(theWindow)) == nil)
  45.         goto ExitNewView;
  46.         
  47.     if ((myStatus = Q3View_SetDrawContext(myView, myDrawContext)) == kQ3Failure)
  48.         goto ExitNewView;
  49.  
  50.     Q3Object_Dispose(myDrawContext);
  51.     
  52.     //    Create and set renderer.
  53.     
  54.     // this would use the wireframe renderer
  55. #if 0
  56.  
  57.     myRenderer = Q3Renderer_NewFromType(kQ3RendererTypeWireFrame);
  58.     if ((myStatus = Q3View_SetRenderer(myView, myRenderer)) == kQ3Failure) {
  59.         goto ExitNewView;
  60.     }
  61.     
  62. #else
  63.  
  64.     // this would use the interactive software renderer
  65.  
  66.     if ((myRenderer = Q3Renderer_NewFromType(kQ3RendererTypeInteractive)) != nil) {
  67.         if ((myStatus = Q3View_SetRenderer(myView, myRenderer)) == kQ3Failure) {
  68.             goto ExitNewView;
  69.         }
  70.         // these two lines set us up to use the best possible renderer,
  71.         // including  hardware if it is installed.
  72.         Q3InteractiveRenderer_SetDoubleBufferBypass(myRenderer, kQ3True);                        
  73.         Q3InteractiveRenderer_SetPreferences(myRenderer, kQAVendor_BestChoice, 0);
  74.  
  75.     }
  76.     else {
  77.         goto ExitNewView;
  78.     }
  79. #endif
  80.  
  81.     Q3Object_Dispose(myRenderer);
  82.     
  83.     //    Create and set camera.
  84.     if ((myCamera = MyNewCamera(theWindow)) == nil)
  85.         goto ExitNewView;
  86.         
  87.     if ((myStatus = Q3View_SetCamera(myView, myCamera)) == kQ3Failure)
  88.         goto ExitNewView;
  89.  
  90.     Q3Object_Dispose(myCamera);
  91.     
  92.     //    Create and set lights.
  93.     if ((myLights = MyNewLights()) == nil)
  94.         goto ExitNewView;
  95.         
  96.     if ((myStatus = Q3View_SetLightGroup(myView, myLights)) == kQ3Failure)
  97.         goto ExitNewView;
  98.         
  99.     Q3Object_Dispose(myLights);
  100.  
  101.     //    Done!!!
  102.     return(myView);
  103.     
  104. ExitNewView:
  105.     //    If any of the above failed, then don't return a view.
  106.     return(nil);
  107. }
  108.  
  109. //----------------------------------------------------------------------------------
  110.  
  111. TQ3DrawContextObject MyNewDrawContext(WindowPtr theWindow)
  112. {
  113.     TQ3DrawContextData        myDrawContextData;
  114.     TQ3MacDrawContextData    myMacDrawContextData;
  115.     TQ3ColorARGB            ClearColor;
  116.     TQ3DrawContextObject    myDrawContext ;
  117.     
  118.     //    Set the background color.
  119.     ClearColor.a = 1.0;
  120.     ClearColor.r = 1.0;
  121.     ClearColor.g = 1.0;
  122.     ClearColor.b = 1.0;
  123.     
  124.     //    Fill in draw context data.
  125.     myDrawContextData.clearImageMethod = kQ3ClearMethodWithColor;
  126.     myDrawContextData.clearImageColor = ClearColor;
  127.     myDrawContextData.paneState = kQ3False;
  128.     myDrawContextData.maskState = kQ3False;
  129.     myDrawContextData.doubleBufferState = kQ3True;
  130.  
  131.     myMacDrawContextData.drawContextData = myDrawContextData;
  132.     
  133.     myMacDrawContextData.window =(CGrafPtr) theWindow;        // this is the window associated with the view
  134.     myMacDrawContextData.library = kQ3Mac2DLibraryNone;
  135.     myMacDrawContextData.viewPort = nil;
  136.     myMacDrawContextData.grafPort = nil;
  137.     
  138.     //    Create draw context and return it, if it’s nil the caller must handle
  139.     myDrawContext = Q3MacDrawContext_New(&myMacDrawContextData);
  140.  
  141.     return myDrawContext ;
  142. }
  143.  
  144. //----------------------------------------------------------------------------------
  145.  
  146. TQ3CameraObject MyNewCamera(WindowPtr theWindow)
  147. {
  148.     TQ3ViewAngleAspectCameraData    perspectiveData;
  149.     TQ3CameraObject                camera;
  150.     
  151.     TQ3Point3D                     from     = { 0.0, 0.0, 3.0 };
  152.     TQ3Point3D                     to         = { 0.0, 0.0, 0.0 };
  153.     TQ3Vector3D                 up         = { 0.0, 1.0, 0.0 };
  154.  
  155.     float                         fieldOfView = 1.0;
  156.     float                         hither         = 0.001;
  157.     float                         yon         = 100;
  158.     
  159.     TQ3Status                    returnVal = kQ3Failure ;
  160.  
  161.  
  162.     perspectiveData.cameraData.placement.cameraLocation     = from;
  163.     perspectiveData.cameraData.placement.pointOfInterest     = to;
  164.     perspectiveData.cameraData.placement.upVector             = up;
  165.  
  166.     perspectiveData.cameraData.range.hither    = hither;
  167.     perspectiveData.cameraData.range.yon     = yon;
  168.  
  169.     perspectiveData.cameraData.viewPort.origin.x = -1.0;
  170.     perspectiveData.cameraData.viewPort.origin.y = 1.0;
  171.     perspectiveData.cameraData.viewPort.width = 2.0;
  172.     perspectiveData.cameraData.viewPort.height = 2.0;
  173.     
  174.     perspectiveData.fov                = fieldOfView;
  175.     perspectiveData.aspectRatioXToY    =
  176.         (float)(theWindow->portRect.right - theWindow->portRect.left) / 
  177.         (float)(theWindow->portRect.bottom - theWindow->portRect.top);
  178.         
  179.     camera = Q3ViewAngleAspectCamera_New(&perspectiveData);
  180.  
  181.     return camera ;
  182. }
  183.  
  184.  
  185. //----------------------------------------------------------------------------------
  186.  
  187. TQ3GroupObject MyNewLights()
  188. {
  189.     TQ3GroupPosition        myGroupPosition;
  190.     TQ3GroupObject            myLightList;
  191.     TQ3LightData            myLightData;
  192.     TQ3DirectionalLightData    myDirectionalLightData;
  193.     TQ3LightObject            myAmbientLight, myFillLight;
  194.     TQ3Vector3D                fillDirection = { 0.0, 0.0, 50.0 };
  195.     TQ3ColorRGB                whiteLight = { 1.0, 1.0, 1.0 };
  196.     
  197.     //    Set up light data for ambient light.  This light data will be used for point and fill
  198.     //    light also.
  199.  
  200.     myLightData.isOn = kQ3True;
  201.     myLightData.color = whiteLight;
  202.     
  203.     //    Create ambient light.
  204.     myLightData.brightness = 1.0;
  205.     myAmbientLight = Q3AmbientLight_New(&myLightData);
  206.     if (myAmbientLight == nil)
  207.         goto ExitMyNewLights;
  208.     
  209.     //    Create fill light.
  210.     myLightData.brightness = 0.2;
  211.     myDirectionalLightData.lightData = myLightData;
  212.     myDirectionalLightData.castsShadows = kQ3False;
  213.     myDirectionalLightData.direction = fillDirection;
  214.     myFillLight = Q3DirectionalLight_New(&myDirectionalLightData);
  215.     if (myFillLight == nil)
  216.         goto ExitMyNewLights;
  217.  
  218.     //    Create light group and add each of the lights into the group.
  219.     myLightList = Q3LightGroup_New();
  220.     if (myLightList == nil)
  221.         goto ExitMyNewLights;
  222.  
  223.     myGroupPosition = Q3Group_AddObject(myLightList, myAmbientLight);
  224.     if (myGroupPosition == 0)
  225.         goto ExitMyNewLights;
  226.  
  227.     myGroupPosition = Q3Group_AddObject(myLightList, myFillLight);
  228.     if (myGroupPosition == 0)
  229.         goto ExitMyNewLights;
  230.  
  231.     Q3Object_Dispose(myAmbientLight);
  232.     Q3Object_Dispose(myFillLight);
  233.  
  234.     //    Done!
  235.     return(myLightList);
  236.     
  237. ExitMyNewLights:
  238.     //    If any of the above failed, then return nothing!
  239.     return(nil);
  240. }
  241.  
  242. TQ3GroupObject MyNewModel()
  243. {
  244.     #define    kNumVertices    5
  245.     #define    kAngleMax        360.0
  246.     #define    kAngleIncr        (kAngleMax / kNumVertices)
  247.  
  248.     TQ3GroupObject        group;
  249.     TQ3GeometryObject    geometry;
  250.     unsigned long        i;
  251.     float                angle,
  252.                         radius;
  253.     TQ3Vertex3D            vertices[kNumVertices];
  254.     TQ3MeshVertex        meshVertices[kNumVertices];
  255.     TQ3MeshFace            meshFace;
  256.     TQ3ShaderObject        illuminationShader;
  257.  
  258.     group    = NULL;
  259.     geometry = NULL;
  260.  
  261.     /* Make a regular group */
  262.     if ((group = Q3DisplayGroup_New()) == NULL)
  263.         return NULL;
  264.  
  265.     /* Add a Phong illumination shader */
  266.     if ((illuminationShader = Q3PhongIllumination_New()) == NULL)
  267.         return NULL;
  268.  
  269.     Q3Group_AddObject(group, illuminationShader);
  270.     Q3Object_Dispose(illuminationShader);
  271.  
  272.  
  273.     /* Create a mesh */
  274.     geometry = Q3Mesh_New();
  275.  
  276.     /* Create a mesh face */
  277.     radius = 1.0;
  278.     for (i = 0, angle = kAngleIncr / 4.0;
  279.         i < kNumVertices;
  280.         i++, angle += kAngleIncr) {
  281.  
  282.         vertices[i].point.x = uMath_Cos_Deg(angle) * radius;
  283.         vertices[i].point.y = uMath_Sin_Deg(angle) * radius;
  284.         vertices[i].point.z = 0.0;
  285.         vertices[i].attributeSet = NULL;
  286.  
  287.         meshVertices[i] = Q3Mesh_VertexNew(geometry, &vertices[i]);        
  288.     }
  289.  
  290.     meshFace = Q3Mesh_FaceNew(geometry, kNumVertices, meshVertices, NULL);
  291.  
  292.     /* Return the mesh in a group */
  293.     Q3Group_AddObject(group, geometry);
  294.     Q3Object_Dispose(geometry);
  295.  
  296.     return group;
  297. }
  298.  
  299.